package com.yan.demo5;


import java.util.concurrent.ArrayBlockingQueue;

/**
 * @author taoge
 * @Date 2024/8/9 14:28
 * @Description
 */
public class Demo5 {
    public static void main(String[] args) {
        ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(10);

        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("生产者开始生产");
                for (int i = 0; i < 11; i++) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() +"生产了" +  i);
                    queue.offer(i);
                }

            }
        } , "生产者A").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("消费者开始消费");
                while(true){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    try {
                        Integer i = queue.take();
                        System.out.println(Thread.currentThread().getName() + "消费了" + i);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        } , "消费者B").start();
    }
}
